import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import { login, getInfo, logout } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

storage.addPlugin(expirePlugin)
const user = {
  state: {
    token: '',
    name: storage.get('userName') || '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    permissions: storage.get('permissions') || []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const result = response.data

          storage.set(ACCESS_TOKEN, result.accessToken, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
          storage.set('userName', result.userName, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
          storage.set('permissions', result.permissions, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', result.accessToken)
          commit('SET_NAME', { name: result.userName, welcome: welcome() })
          commit('SET_PERMISSIONS', result.permissions)

          // commit('SET_ROLES', role)
          // commit('SET_INFO', result)
          // commit('SET_NAME', { name: result.userName, welcome: welcome() })
          // commit('SET_AVATAR', result.avatar)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        // 请求后端获取用户信息 /api/user/info
        resolve()
        // getInfo().then(response => {
        //   const { result } = response
        //   if (result.role && result.role.permissions.length > 0) {
        //     const role = { ...result.role }
        //     role.permissions = result.role.permissions.map(permission => {
        //       const per = {
        //         ...permission,
        //         actionList: (permission.actionEntitySet || {}).map(item => item.action)
        //        }
        //       return per
        //     })
        //     role.permissionList = role.permissions.map(permission => { return permission.permissionId })
        //     // 覆盖响应体的 role, 供下游使用
        //     result.role = role

        //     commit('SET_ROLES', role)
        //     commit('SET_INFO', result)
        //     commit('SET_NAME', { name: result.name, welcome: welcome() })
        //     commit('SET_AVATAR', result.avatar)
        //     // 下游
        //     resolve(result)
        //   } else {
        //     reject(new Error('getInfo: roles must be a non-null array !'))
        //   }
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        // console.log('登出>>>>>>>>>>清state')
        // logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_NAME', { name: '', welcome: '' })
          commit('SET_PERMISSIONS', [])
          storage.remove(ACCESS_TOKEN)
          storage.remove('userName')
          storage.remove('permissions')
          resolve()
        // }).catch((err) => {
        //   console.log('logout fail:', err)
        //   // resolve()
        // }).finally(() => {
        // })
      })
    }

  }
}

export default user
